<?php
/*
 * Created on Oct 9, 2009
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
?>
<script type="text/javascript">
$(function() {
	$("a.ajax-edit").click(function(){
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 325,
			width: 400,
			title: 'Edit Role',
			modal: true,
			buttons: {
				'Save': function() {
					// Validation
					jQuery("#roleForm").validate({
						submitHandler: function(form) {
							$(form).ajaxForm({
								success: function(data){
									if(data.status=="success"){
										$("#role_name"+data.id).html(data.name);
										$("#role_desc"+data.id).html(data.description);
										$("#role_date"+data.id).html(data.modified_date);
										//alert("You have just updated a record.");
									}

								},
								dataType: 'json'
							});
						}
					});
		            $('#roleForm').submit();
					$("#dialog").dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#userForm input').val('').removeClass('ui-state-error');
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$("a.ajax-delete").click(function(){
		var delLink = $(this).attr("href");
		var temp = new Array();
		temp = delLink.split('/');
		var id = temp[temp.length-1];
		$("#dialog").html('<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span>This item will be permanently deleted and cannot be recovered. Are you sure?</p>')
		$("#dialog").dialog({
			title: 'Delete "' + $(this).attr('title') + '"',
			autoOpen: true,
			height: 180,
			buttons: {
				'Delete': function() {
					// Submit delete item
					$.get(delLink, function(){
						alert("You have just removed a record.");
					}, "json");
					$("#role"+id).remove();
					$(this).dialog('close');
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			}
		});
		$("#dialog").dialog("open");
		return false;
	});

	$('#addRole').click(function() {
		$("#dialog").dialog({
			bgiframe: true,
			autoOpen: false,
			height: 300,
			width: 400,
			title: 'Create Role',
			modal: true,
			buttons: {
				'Create': function() {
					// Validation
					jQuery("#roleForm").validate({
						submitHandler: function(form) {
							$(form).ajaxForm({
								success: function(data){
									var $table = $("#roleBrowse tbody");
									var $roleId = data.id;
									$.ajax({
									  url: "<?=site_url(array('c'=>'cms_user', 'm'=>'find_role', 'role_id' => $roleId))?>",
									  cache: false,
									  success: function(html){
									    $table.append(html);
									  }
									});
									$("#dialog").dialog('close');
								},
								dataType: 'json'
							});
						}
					});
		            $('#roleForm').submit();
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				$('#roleForm input').val('').removeClass('ui-state-error');
			}
		});
		$('#dialog').load($(this).attr('href'));
		$("#dialog").dialog("open");
		return false;
	});
});
</script>
<div style="text-align: right;">
	<a id="addRole" href="<?=site_url(array("c" => "cms_user", "m" => "add_role"))?>">
		<img src="<?=image_url();?>function/add.gif" width="106" height="31" border="0">
	</a>
</div>
<table id="roleBrowse" width="100%" border="0" cellpadding="0" cellspacing="0" class="cont-table-list">
	<thead>
		<tr>
			<th class="small">Edit</th>
			<th class="small">Delete</th>
			<th class="left">Role Name</th>
			<th class="left">Description</th>
			<th class="left">Created Date</th>
		</tr>
	</thead>
	<tbody>
	<?php $i = 1; ?>
	<?php foreach($roles as $item):?>
	<tr id="role<?=$item['id']?>" class="<?= ($i==2) ? 'odd' : 'even'; ?>">
		<td class="small">
			<a class="ajax-edit" href="<?= site_url(array("c" => "cms_user", "m" => "edit_role", "role_id" => $item['id']));?>"><img src="<?=public_base_url()?>admin/images/icons/edit.gif" width="30" height="30" border="0"></a></td>
		<td class="small">
			<a class="ajax-delete" href="<?= site_url(array("c" => "cms_user", "m" => "delete_role", "role_id" => $item['id']));?>"><img src="<?=public_base_url()?>admin/images/icons/del.gif" width="30" height="30" border="0"></a></td>
		<td id="role_name<?=$item['id']?>"><?=$item['name']?></td>
		<td id="role_desc<?=$item['id']?>"><?=$item['description']?></td>
		<td id="role_date<?=$item['id']?>"><?=$item['modified_date']?></td>
	</tr>
	<? $i++;?>
	<?php endforeach;?>
	</tbody>
</table>
<div id="temp"></div>